package com.lagou.web;

import com.lagou.domain.User;
import com.lagou.service.CheckLogin;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Administrator
 */

public class Register extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取表单信息 用户名和密码
        String username = req.getParameter("username");
        System.out.println("username : [" + username+"]");

        String password = req.getParameter("password");
        System.out.println("password : [" + password+"]");

        if(username == null || password == null || username.isBlank() || password.isBlank()){
            System.out.println("请输入用户名和密码!");
            //响应给页面
            resp.getWriter().write("请输入用户名和密码!");
            return;
        }
        //去掉空格
        String userName = username.trim();
        String passWord = password.trim();

        User user = new User(userName,passWord);
        //2.判断用户名是否存在
        CheckLogin checkLogin = new CheckLogin(user);
        boolean flag = checkLogin.register();
        if(flag){
            System.out.println("注册成功");
            //响应给页面
            resp.getWriter().write("注册成功");
        }else{
            System.out.println("用户名已经存在，注册失败");
            //响应给页面
            resp.getWriter().write("用户名已经存在，注册失败");
        }
    }
}
